package app.utils
{
    import copyengine.infoTree.meta.CESFileMeta;

    import flash.display.BitmapData;
    import flash.display.PNGEncoderOptions;
    import flash.filesystem.File;
    import flash.filesystem.FileMode;
    import flash.filesystem.FileStream;
    import flash.geom.Rectangle;
    import flash.utils.ByteArray;

    public class AppFileUtils
    {
        public function AppFileUtils()
        {
        }

        /**
         * 导出显示树Meta文件
         */
        public static function exportFileMeta(_fileMeta:CESFileMeta, _outputFilePath:String, _fileName:String):void
        {
//            var serializedObj:Object = GeneralUtils.serializeObject(_fileMeta);
            var fileByteArray:ByteArray = new ByteArray();
            fileByteArray.writeObject(_fileMeta);
            fileByteArray.compress();
            writeByteArrayToPath(fileByteArray, _outputFilePath + "/" + _fileName + ".ce");
        }

        //================================================//
        //===============   UTILS  WRITE   =====================//
        //================================================//

        private static function writeByteArrayToPath(_data:ByteArray, _path:String):void
        {
            var file:File = new File(_path);
            var fileStream:FileStream = new FileStream();
            fileStream.open(file, FileMode.WRITE);
            fileStream.writeBytes(_data);
            fileStream.close();
        }

        public static function writeImgToPath(_bitmapData:BitmapData, _path:String):void
        {
            var bytes:ByteArray = _bitmapData.encode(new Rectangle(0, 0, _bitmapData.width, _bitmapData.height),
                    new PNGEncoderOptions());
            writeByteArrayToPath(bytes, _path);
        }

        private static function writeStringToPath(_str:String, _path:String):void
        {
            var bytes:ByteArray = new ByteArray();
            bytes.writeMultiByte(_str, "utf-8");
            writeByteArrayToPath(bytes, _path);
        }

    }
}
